package com.leap.meeting.view;

import com.leap.meeting.dao.AdminDAO;
import com.leap.meeting.dao.UserDAO;
import com.leap.meeting.dao.imple.AdminDAOimple;
import com.leap.meeting.dao.imple.UserDAOimple;
import com.leap.meeting.pojo.Admin;
import com.leap.meeting.pojo.User;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Array;
import java.util.*;
import java.util.List;

public class Login extends JFrame implements MouseListener {

    //设置窗口主题样式
    static {
            String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        try {
            UIManager.setLookAndFeel(lookAndFeel);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedLookAndFeelException e) {
            throw new RuntimeException(e);
        }
    }

    static TreeMap<String, Admin> adminsMap = new TreeMap<>();
    static TreeMap<String, User> usersMap = new TreeMap<>();
    static private AdminDAOimple adminDAOimple = new AdminDAOimple();
    static private UserDAOimple userDAOimple = new UserDAOimple();

    Font f1 = new Font("微软雅黑", Font.BOLD,30);
    Font f2 = new Font("微软雅黑", Font.PLAIN, 20);
    Font f3 = new Font("微软雅黑", Font.PLAIN, 17);
    Font f4 = new Font("微软雅黑", Font.PLAIN, 15);

    JButton login = new JButton("登录");
    JButton register = new JButton("注册");
    JButton quit = new JButton("退出");

    JTextField username = new JTextField();
    JPasswordField password = new JPasswordField();
//    JTextField code = new JTextField();

    ButtonGroup group = new ButtonGroup();
    JRadioButton rb1 = new JRadioButton("用户",true);
    JRadioButton rb2 = new JRadioButton("管理员");

    //注册窗口
    JDialog insertJDialog;
    JTextField accountField = new JTextField();
    JTextField passwordField = new JTextField();
    JTextField realNameField = new JTextField();
    JTextField apartmentField = new JTextField();
    JTextField dutyField = new JTextField();
    JTextField phoneField = new JTextField();
    JTextField emailField = new JTextField();
    JButton yes = new JButton("新增");
    JButton no = new JButton("取消");
    JLabel accountRule = new JLabel("账号至少三位");
    JLabel passwordRule = new JLabel("密码至少三位");
    JLabel phoneRule =new JLabel("请输入正确的手机号！");
    JLabel emailRule= new JLabel("请输入正确的邮箱！");

    public Login(){
        InitMap();
        InitJFrame();
        this.setVisible(true);
    }

    private void InitJFrame(){
        this.setTitle("登录界面");
        this.setSize(600, 550);
        this.setLocationRelativeTo(null);//设置窗口居中
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setResizable(false);//设置不可以通过拖动边框随意改变窗口大小

        JLabel j1 = new JLabel("会议管理系统");
        j1.setBounds(this.getWidth()/2 - 125, 10, 300, 100);
        j1.setFont(f1);

        JLabel j2 = new JLabel("账号");
        j2.setBounds(100, 100, 50, 50);
        j2.setFont(f2);
        username.setBounds(150,110,300,30);
        username.setFont(f3);

        JLabel j3 = new JLabel("密码");
        j3.setBounds(100, 200, 50, 50);
        j3.setFont(f2);
        password.setBounds(150,210,300,30);
        password.setFont(f3);

        JLabel j4 = new JLabel("身份");
        j4.setBounds(100, 280, 50, 50);
        j4.setFont(f2);

        rb1.setBounds(180, 280, 100, 50);
        rb2.setBounds(300, 280, 100, 50);
        rb1.setFont(f3);
        rb2.setFont(f3);

        group.add(rb1);
        group.add(rb2);

        login.setBounds(300, 350, 150, 50);
        register.setBounds(100,350,150,50);
        quit.setBounds(450, 10, 100, 50);
        login.setFont(f2);
        register.setFont(f2);
        quit.setFont(f2);
        login.addMouseListener(this);
        register.addMouseListener(this);
        quit.addMouseListener(this);
        yes.addMouseListener(this);
        no.addMouseListener(this);

        this.add(j1);
        this.add(j2);
        this.add(j3);
        this.add(j4);
        this.add(rb1);
        this.add(rb2);
        this.add(login);
        this.add(register);
        this.add(quit);

        this.add(username);
        this.add(password);

    }

    private void InitMap(){
        List<Admin> admins = adminDAOimple.selectAll();
        List<User> users = userDAOimple.selectAll();
        for (Admin admin : admins) {
            adminsMap.put(admin.getAccount(), admin);
        }
        for (User user : users) {
            usersMap.put(user.getAccount(), user);
        }

        Set<Map.Entry<String, Admin>> entries = adminsMap.entrySet();
        for (Map.Entry<String, Admin> entry : entries) {
            System.out.println(entry);
        }
        System.out.println("==========");
        Set<Map.Entry<String, User>> entries2 = usersMap.entrySet();
        for (Map.Entry<String, User> entry : entries2) {
            System.out.println(entry);
        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj = e.getSource();
        if(obj == login){
            String acc = username.getText();
            String pas = password.getText();
            Set<Map.Entry<String, Admin>> entries = adminsMap.entrySet();
            Set<Map.Entry<String, User>> entries2 = usersMap.entrySet();
            System.out.println(rb1.isSelected());
            System.out.println(rb2.isSelected());
            int flag=0;
            if (rb1.isSelected()) {
                for (Map.Entry<String, User> userEntry : entries2) {
                    if (acc.equals(userEntry.getKey())) {
                        if (pas.equals(userEntry.getValue().getPassword())) {
                            System.out.println("登录成功");
                            new Main_User(acc);
                            this.dispose();
                            flag = 1;
                            break;
                        }
                    }
                }
                if (flag == 0){
                    System.out.println("账号或者密码错误");
                }
            }else if (rb2.isSelected()) {
                for (Map.Entry<String, Admin> entry : entries) {
                    if (acc.equals(entry.getKey())){
                        if (pas.equals(entry.getValue().getPassword())){
                            System.out.println("登录成功");
                            new Main_Admin(acc);
                            this.dispose();
                        }
                    }
                }
                if (flag == 0){
                    System.out.println("账号或者密码错误");
                }
            }else {
                //弹窗报错
            }
        }else if (obj == register){
            insertUser();
        }else if (obj == yes){
            accountRule.setVisible(false);
            passwordRule.setVisible(false);
            phoneRule.setVisible(false);
            emailRule.setVisible(false);
            if (RegexCheck()){
                if (isAllFilled()){
                    User user = userDAOimple.selectByAccount(accountField.getText());
                    if (user == null){
                        user = new User();
                        user.setAccount(accountField.getText());
                        user.setPassword(passwordField.getText());
                        user.setRealName(realNameField.getText());
                        user.setApartment(apartmentField.getText());
                        user.setDuty(dutyField.getText());
                        user.setPhone(phoneField.getText());
                        user.setEmail(emailField.getText());
                        int insert = userDAOimple.insert(user);
                        if (insert > 0){
                            System.out.println("新增用户成功");
                        }else {
                            popWin("发生错误");
                            return;
                        }
                    }else{
                        popWin("账号重复！");
                    }
                }else {
                    popWin("请将用户信息补充完整！");
                    return;
                    //请填满信息
                }
            }else {
                //报错
                return;
            }
            this.InitMap();
            insertJDialog.dispose();
        }else if (obj == no){
            accountRule.setVisible(false);
            passwordRule.setVisible(false);
            phoneRule.setVisible(false);
            emailRule.setVisible(false);
            insertJDialog.dispose();
        }else if(obj == quit){
            this.dispose();
            System.exit(0);
        }
    }

    private boolean isAllFilled(){
        if(!accountField.getText().isEmpty()){
            if (!passwordField.getText().isEmpty()){
                if (!phoneField.getText().isEmpty()){
                    if (!emailField.getText().isEmpty()){
                        if (!realNameField.getText().isEmpty()){
                            if (!apartmentField.getText().isEmpty()){
                                if (!dutyField.getText().isEmpty()){
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    private void insertUser(){
        insertJDialog = new JDialog();
        insertJDialog.setTitle("新增用户");
        insertJDialog.setSize(500, 500);
        insertJDialog.setLocationRelativeTo(null);
        insertJDialog.setLayout(null);
        insertJDialog.setAlwaysOnTop(true);
        insertJDialog.setResizable(false);

        JLabel accountLabel = new JLabel("账号");
        JLabel passwordLabel = new JLabel("密码");
        JLabel realNameLabel = new JLabel("姓名");
        JLabel apartmentLabel = new JLabel("部门");
        JLabel dutyLabel = new JLabel("职位");
        JLabel phoneLabel = new JLabel("手机号");
        JLabel emailLabel = new JLabel("邮箱");

        accountField.setFont(f3);
        passwordField.setFont(f3);
        realNameField.setFont(f3);
        apartmentField.setFont(f3);
        dutyField.setFont(f3);
        phoneField.setFont(f3);
        emailField.setFont(f3);

        accountLabel.setFont(f3);
        passwordLabel.setFont(f3);
        realNameLabel.setFont(f3);
        apartmentLabel.setFont(f3);
        dutyLabel.setFont(f3);
        phoneLabel.setFont(f3);
        emailLabel.setFont(f3);

        accountRule.setFont(f4);
        passwordRule.setFont(f4);
        phoneRule.setFont(f4);
        emailRule.setFont(f4);

        yes.setFont(f3);
        no.setFont(f3);

        accountLabel.setBounds(30, 10, 100, 50);
        passwordLabel.setBounds(30, 60, 100, 50);
        realNameLabel.setBounds(30, 110, 100, 50);
        apartmentLabel.setBounds(30, 160, 100, 50);
        dutyLabel.setBounds(30, 210, 100, 50);
        phoneLabel.setBounds(30, 260, 100, 50);
        emailLabel.setBounds(30, 310, 100, 50);

        accountField.setBounds(100, 20, 150, 30);
        passwordField.setBounds(100, 70, 150, 30);
        realNameField.setBounds(100, 120, 150, 30);
        apartmentField.setBounds(100, 170, 150, 30);
        dutyField.setBounds(100, 220, 150, 30);
        phoneField.setBounds(100, 270, 200, 25);
        emailField.setBounds(100, 320, 200, 25);

        accountRule.setBounds(260, 20, 150 ,25);
        passwordRule.setBounds(260, 70, 150 ,25);
        phoneRule.setBounds(320, 270, 200 ,25);
        emailRule.setBounds(320, 320, 150 ,25);

        accountRule.setForeground(Color.RED);
        passwordRule.setForeground(Color.RED);
        phoneRule.setForeground(Color.RED);
        emailRule.setForeground(Color.RED);
        accountRule.setVisible(false);
        passwordRule.setVisible(false);
        phoneRule.setVisible(false);
        emailRule.setVisible(false);

        accountField.setText("");
        passwordField.setText("");
        realNameField.setText("");
        apartmentField.setText("");
        dutyField.setText("");
        phoneField.setText("");
        emailField.setText("");

        yes.setBounds(30, 400, 100, 40);
        no.setBounds(220, 400, 100, 40);

        insertJDialog.add(accountField);
        insertJDialog.add(passwordField);
        insertJDialog.add(realNameField);
        insertJDialog.add(apartmentField);
        insertJDialog.add(dutyField);
        insertJDialog.add(phoneField);
        insertJDialog.add(emailField);

        insertJDialog.add(accountLabel);
        insertJDialog.add(passwordLabel);
        insertJDialog.add(realNameLabel);
        insertJDialog.add(apartmentLabel);
        insertJDialog.add(dutyLabel);
        insertJDialog.add(phoneLabel);
        insertJDialog.add(emailLabel);

        insertJDialog.add(accountRule);
        insertJDialog.add(passwordRule);
        insertJDialog.add(phoneRule);
        insertJDialog.add(emailRule);

        insertJDialog.add(yes);
        insertJDialog.add(no);

        insertJDialog.setModal(true);
        insertJDialog.setVisible(true);
    }

    private boolean RegexCheck() {
        String regex_acc = ".{3,}";
        //至少三位
        String regex_pas = ".{3,}";
        String regex_phone = "1\\d{10}";
        String regex_email = "\\w+@[\\w&&[^_]]+(\\.[a-zA-Z]{2,3})";
        int flag =0;
        if(!accountField.getText().matches(regex_acc)){
            flag=1;
            accountRule.setVisible(true);
        }
        if (!passwordField.getText().matches(regex_pas)){
            flag=1;
            passwordRule.setVisible(true);
        }
        if (!phoneField.getText().matches(regex_phone)){
            flag=1;
            phoneRule.setVisible(true);
        }
        if (!emailField.getText().matches(regex_email)){
            flag=1;
            emailRule.setVisible(true);
        }
        if (flag == 0){
            return true;
        }else {
            return false;
        }
    }

    private void popWin(String str){
        JDialog dialog = new JDialog();
        dialog.setSize(350, 200);
        dialog.setAlwaysOnTop(true);
        dialog.setLocationRelativeTo(null);
        dialog.setLayout(null);
        JLabel label = new JLabel(str);
        label.setFont(f2);
        label.setBounds(70, 50, 300, 40);
        dialog.getContentPane().add(label);
        dialog.setModal(true);
        dialog.setVisible(true);
    }


    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
